var common = {

    doAjax: function(url, params, method) {
        var deferred = $.Deferred();

        if (typeof(url) === 'object') {
            return deferred.resolve(url);
        }

        $.ajax({
            type: method || 'POST',
            url: url,
            traditional: true,
            data: params || {},
            dataType: 'json',
            success: function(resp) {
                try {
                    if (typeof resp == 'string') {
                        resp = JSON.parse(resp);
                    }
                } catch (e) {
                    log.error(e);
                }
                deferred.resolve(resp);
            },
            error: function(resp) {
                deferred.reject(resp);
            }
        });
        return deferred.promise();
    },

    fetch: function(options) {
        var def = $.Deferred();
        if (!options.url) {
            return def.resolve(options.parser && options.parser());
        }
        utils.doAjax(options.url, options.data, options.method).done(function(res) {
            var result = options.parser(res) || res;
            def.resolve(result);
        }).fail(function(res) {
            def.reject(res);
        });

        return def.promise();
    },

    /**
     * 同步ajax方法
     * @param  {String} url    请求URL
     * @param  {Object} params 请求参数
     * @param  {String} method 请求方法
     */
    doSyncAjax: function(url, params, method) {
        var resp = $.ajax({
            type: method || 'POST',
            url: url,
            traditional: true,
            data: params || {},
            dataType: 'json',
            cache: false,
            async: false,
            error: function(resp) {
                // console.log("doSyncAjax error:----------");
                // console.log(resp);
                // console.log("----------:doSyncAjax error");
                var result = JSON.parse(resp.responseText);
                //console.log(result);
                //如果未登录跳转至登录页面
                if (typeof result.loginURL != 'undefined' && resp.loginURL != '') {
                    //console.log(result.loginURL);
                    window.location.href = result.loginURL;
                }
            }
        });
        if (resp.status != 200) {
            return {};
        }
        var result = JSON.parse(resp.responseText);
        //如果未登录跳转至登录页面
        if (typeof result.loginURL != 'undefined' && resp.loginURL != '') {
            window.location.href = result.loginURL;
        }
        return result;
    },
};